/*******************************************************************************
master.do

This code creates all the tables, figures, and statistics reported in Eliason
et al. (2024)
*******************************************************************************/

/******************
****Switches

This code relies on publicly available data, data from USRDS, and Medicare data
from the NBER. Use the global macros and switches below to run the code that uses
the data sources you are interested in. Note that you may change your directory
for different data sources to point to the location of this file, which should 
remain with the subfolders of this replication package. Note that for the USRDS
code to run, the raw claims data must be added to the "data\raw" subfolder.

******************/

*Put your paths here
global publicbasepath "C:\Users\riley\Dropbox\DialysisAmbulanceFraud\JPE Submission\Final Submission\replication\"
global NBERbasepath "/homes/nber/jetson-dua57641/gruber-DUA57641/jetson-dua57641/replication/"
global USRDSbasepath "X:\shared\ambulance\Work\replicationpackage\"

*Indicate what code to run
global runpublic = 0 // 0 to skip running code using public data, 1 to run it
global runNBER = 0 // 0 to skip running code using NBER Medicare data, 1 to run it
global runUSRDS = 0 // 0 to skip running code using USDRS data, 1 to run it


*** Edit below here at your own risk *******************************************

set scheme s2color

*Public data
if ${runpublic}==1 {
	
	*Set macros
	global dopath "${publicbasepath}code/"
	global rawdatapath "${publicbasepath}data/raw/"
	global cleandatapath "${publicbasepath}data/cleaned/"
	global logpath "${publicbasepath}logs/"
	global outpath "${publicbasepath}output/"
	global estpath "${publicbasepath}estimates/"

	do "${dopath}1_1_misc_data_prep.do"
	*Inputs: Indicted_NPIsxfirm.csv, Treatment_Dates.csv, zip_doj_crosswalk.dta, WorkHours.xlsx, MUP`y'.csv
	*Output Data: Indicted_NPIs.dta, DOJ_data_district.dta, DOJcounty_Xwalk.dta, DOJstate_Xwalk.dta, WorkHours.dta, MUP.dta
	*Output Stats:
	*Output Tables:
	*Output Figures:
	
	do "${dopath}1_2_case_counts.do"
	*Inputs: CaseData.xlsx
	*Output Data:
	*Output Stats: Number of firms subject to litigation
	*Output Tables:
	*Output Figures:
	
	do "${dopath}1_3_recoveries.do"
	*Inputs: RecoveryData_full.xlsx
	*Output Data:
	*Output Stats: Amount and shares of ordered repayments recovered
	*Output Tables:
	*Output Figures:
	
	do "${dopath}1_4_LIONS.do"
	*Inputs: gs_case_cause_act.txt, gs_assignment.txt
	*Output Data:
	*Output Stats: Summary statistics on US Attorney health care fraud cases
	*Output Tables:
	*Output Figures:
	
}

if ${runNBER}==1 {

	*Set macros
	global dopath "${NBERbasepath}code/"
	global rawdatapath "/disk/aging/medicare/data/harm/20pct/"
	global cleandatapath "${NBERbasepath}data/cleaned/"
	global logpath "${NBERbasepath}logs/"
	global outpath "${NBERbasepath}output/"
	global estpath "${NBERbasepath}estimates/"

	do "${dopath}2_1_clean_claims.do"
	*Inputs: carl`YEAR'.dta, Indicted_NPIs.dta, zip_doj_crosswalk.dta, DOJ_data_district.dta
	*Output Data: ambulancetaxis_claims_0719.dta, ambulancetaxis_firmpanel.dta
	*Output Stats:
	*Output Tables:
	*Output Figures:

	do "${dopath}2_2_district_regs.do"
	*Inputs: ambulancetaxis_firmpanel_enforcement.dta
	*Output Data:
	*Output Stats: P-value reported in Table A32
	*Output Tables: 8
	*Output Figures: 11

	do "${dopath}2_3_firm_eventstudy.do"
	*Inputs: ambulancetaxis_firmpanel_enforcement.dta
	*Output Data:
	*Output Stats:
	*Output Tables:
	*Output Figures: 12, A19

	do "${dopath}2_4_describe_enforcement.do"
	*Inputs: ambulancetaxis_firmpanel_enforcement.dta
	*Output Data:
	*Output Stats: Number of and payments to sued firms in NBER data
	*Output Tables:
	*Output Figures:

	do "${dopath}2_5_count_firms.do"
	*Inputs: ambulancetaxis_claims_0719.dta
	*Output Data:
	*Output Stats: Total firms and patients in NBER data
	*Output Tables:
	*Output Figures:

	do "${dopath}2_6_specialization.do"
	*Inputs: carl`YEAR'.dta, zip_doj_crosswalk.dta, DOJ_data_district.dta
	*Output Data:
	*Output Stats:
	*Output Tables:
	*Output Figures: A13
	
}

if ${runUSRDS}==1 {

	*Set macros
	global dopath "${USRDSbasepath}code/"
	global rawdatapath "${USRDSbasepath}data/raw/"
	global cleandatapath "${USRDSbasepath}data/cleaned/"
	global logpath "${USRDSbasepath}logs/"
	global outpath "${USRDSbasepath}output/"
	global estpath "${USRDSbasepath}estimates/"
	global pat_controls inc_diabetes inc_hyper i.bmi_bin i.gfr_bin ///
						male nhwhite black hisp asian rother c.pat_age c.pat_age2 c.pat_age3 ///
						c.dial_tenure c.dial_tenure2 c.dial_tenure3 i.inc_hgb_group ///
						high_album inc_cancer inc_drug inc_drinker inc_smoker inc_assist ///
						inc_copd inc_ashd inc_pvd inc_ischem inc_chd 
	global fac_controls c.fac_age c.fac_age2 i.state i.income_bin ib3.newchain i.fac_free

	do ${dopath}3_1_clean_ridelevel.do
	*Inputs: ridelevel_data_complete.dta, DOJcounty_Xwalk.dta, DOJstate_Xwalk.dta, Indicted_NPIs.dta, DOJ_data_district.dta
	*Output Data: ridelevel_emerg.dta, ridelevel.dta, dist_data.dta
	*Output Stats: Total spending, rides, and firms for emergent and nonemergent rides and fraudulent firms
	*Output Tables:
	*Output Figures:

	do ${dopath}3_2_ridelevel_analysis.do
	*Inputs: ridelevel.dta
	*Output Data: exit_at_priorauth.dta
	*Output Stats: Number of riders that stop at PA, Counterfactual savings
	*Output Tables:
	*Output Figures: 1-2, A21-23

	do ${dopath}3_3_clean_patientlevel.do
	*Inputs: patient_panel_complete.dta, DOJstate_Xwalk.dta, DOJcounty_Xwalk.dta, DOJ_data_district.dta, exit_at_priorauth.dta
	*Output Data: patientlevel.dta
	*Output Stats:
	*Output Tables:
	*Output Figures:

	do ${dopath}3_4_priorauth_patlevel.do
	*Inputs: patientlevel.dta
	*Output Data:
	*Output Stats: Number of riders under prior authorization
	*Output Tables: 1, 4-5, 7, A8, A23
	*Output Figures: 8-9, A11b

	do ${dopath}3_5_district_regressions.do
	*Inputs: dist_data.dta
	*Output Data:
	*Output Stats: Incapacitation share
	*Output Tables: 2-3, 6, A10-3, A15-21, A32
	*Output Figures: 3-6, A8-10, A18, A22

	do ${dopath}3_6_emergency_analysis.do
	*Inputs: ridelevel_emerg.dta
	*Output Data: firmlevel.dta
	*Output Stats: Count of non-emergent-only firms
	*Output Tables:
	*Output Figures: 7, A11a, A20

	do ${dopath}3_7_denial_analysis.do
	*Inputs: ridelevel_data_deny_complete.dta, Indicted_NPIs.dta
	*Output Data:
	*Output Stats: Denial rates around prior authorization
	*Output Tables:
	*Output Figures: 10, A12

	do ${dopath}3_8_general_deterrence.do
	*Inputs: dist_data.dta, WorkHours.dta
	*Output Data:
	*Output Stats:
	*Output Tables: 9
	*Output Figures:

	do ${dopath}3_9_litigation_trends.do
	*Inputs: dist_data.dta
	*Output Data:
	*Output Stats:
	*Output Tables:
	*Output Figures: A2-A3

	do ${dopath}3_10_CS_estimator.do
	*Inputs: dist_data.dta
	*Output Data:
	*Output Stats:
	*Output Tables: A2-3, A14
	*Output Figures: A4, A7

	do ${dopath}3_11_stacked_regression.do
	*Inputs: dist_data.dta
	*Output Data:
	*Output Stats:
	*Output Tables: A4-5
	*Output Figures: A5

	do ${dopath}3_12_imputation_estimator.do
	*Inputs: dist_data.dta
	*Output Data: 
	*Output Stats:
	*Output Tables: A6-7
	*Output Figures: A6

	do ${dopath}3_13_litigation_patlevel.do
	*Inputs: patientlevel.dta
	*Output Data:
	*Output Stats:
	*Output Tables: A9, A26-31
	*Output Figures: A14-7

	do ${dopath}3_14_spillovers.do
	*Inputs: dist_data.dta
	*Output Data:
	*Output Stats:
	*Output Tables: A25
	*Output Figures:

	do ${dopath}3_15_firmlevel_analysis.do
	*Inputs: firmlevel.dta, Indicted_NPIs.dta, MUP.dta
	*Output Data:
	*Output Stats: Reduction in spending by size
	*Output Tables: A33
	*Output Figures:
	
}